package com.ljl.system.service.impl;

import com.ljl.system.domain.Role;
import com.ljl.system.mapper.MenuMapper;
import com.ljl.system.service.IRoleService;
import com.ljl.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {

    @Autowired
    private MenuMapper menuMapper;

    @Override
    @Transactional
    public void save(Role role) {
        super.save(role);
        addData(role);
    }


    private void addData(Role role){
        List<Long> menus = role.getMenus();
        if(menus.size() > 0){
            menuMapper.saveRoleMenu(role.getId(),menus);
        }
        List<Long> permissions = role.getPermissions();
        if(permissions.size() > 0){
            menuMapper.saveRolePermission(role.getId(),permissions);
        }
    }

    @Override
    @Transactional
    public void del(Long id) {
        menuMapper.delMenuByRoleId(id);
        menuMapper.delPermissionByRoleId(id);
        menuMapper.delEmployeeByRoleId(id);
        super.del(id);
    }

    @Override
    @Transactional
    public void update(Role role) {
        menuMapper.delMenuByRoleId(role.getId());
        menuMapper.delPermissionByRoleId(role.getId());
        addData(role);
        super.update(role);
    }

    @Override
    @Transactional
    public void batchRemove(List<Long> ids) {
        for (Long id : ids) {
            del(id);
        }
       // super.batchRemove(ids);
    }
}
